const storageManager = require('../storage');

/**
 * 工作流管理工具
 */
module.exports = {
  name: 'workflow',
  description: '工作流管理工具，用于添加、查询和列出核心功能的业务流程',
  parameters: {
    type: 'object',
    properties: {
      action: {
        type: 'string',
        enum: ['list', 'add', 'get'],
        description: '要执行的操作: list(列出所有工作流), add(添加新工作流), get(获取特定工作流)'
      },
      name: {
        type: 'string',
        description: '工作流名称，用于添加或获取工作流'
      },
      description: {
        type: 'string',
        description: '工作流描述，用于添加新工作流'
      },
      steps: {
        type: 'array',
        items: {
          type: 'object',
          properties: {
            name: { type: 'string' },
            description: { type: 'string' },
            actor: { type: 'string' },
            action: { type: 'string' }
          }
        },
        description: '工作流步骤，用于添加新工作流'
      },
      diagram: {
        type: 'string',
        description: '工作流图表，可以是PlantUML、Mermaid等格式的文本'
      },
      tags: {
        type: 'array',
        items: {
          type: 'string'
        },
        description: '工作流标签，用于分类和搜索'
      }
    },
    required: ['action']
  },
  handler: async ({ action, name, description, steps, diagram, tags }) => {
    try {
      switch (action) {
        case 'list':
          const workflows = storageManager.getWorkflows();
          return {
            workflows,
            count: workflows.length
          };
        
        case 'add':
          if (!name) {
            throw new Error('添加工作流需要提供工作流名称');
          }
          
          const newWorkflow = storageManager.addWorkflow({
            name,
            description: description || '',
            steps: steps || [],
            diagram: diagram || '',
            tags: tags || []
          });
          
          return {
            message: `工作流 ${name} 添加成功`,
            workflow: newWorkflow
          };
        
        case 'get':
          if (!name) {
            throw new Error('获取工作流需要提供工作流名称');
          }
          
          const workflowsList = storageManager.getWorkflows();
          const workflow = workflowsList.find(w => w.name === name);
          
          if (!workflow) {
            throw new Error(`未找到工作流: ${name}`);
          }
          
          return {
            workflow
          };
        
        default:
          throw new Error(`未知操作: ${action}`);
      }
    } catch (error) {
      return {
        error: error.message
      };
    }
  }
}; 